<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑店主</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/temp/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/temp/css/frame.css">

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/static/temp/js/xadmin.js"></script>
</head>
<body>           
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>编辑店主</legend>
</fieldset>

<form class="layui-form layui-form-pane" action="" lay-filter="example" style="padding: 20px">
    <input type="hidden" name="id" value="{$admin.id}" lay-verify="id">

    <div class="layui-form-item">
        <label class="layui-form-label">门店名称</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="nickname" value="{$admin.nickname}" lay-verify="nickname" autocomplete="off" placeholder="请输入门店名称" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 2-20个字符
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="username" value="{$admin.username}" lay-verify="username" autocomplete="off" placeholder="请输入账号" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 5-20个字符
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="password" name="password" lay-verify="password" autocomplete="off" placeholder="请输入密码" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 5-20个字符（留空为不修改密码）
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">新人福利金</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="number" name="money_gold" value="{$admin.money_gold}" lay-verify="money_gold" autocomplete="off" placeholder="请输入新人福利金" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">福利金时效</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="number" name="gold_time" value="{$admin.gold_time}" lay-verify="gold_time" autocomplete="off" placeholder="请输入福利金有效期" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 单位：天（0表示永久）
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">积分比例</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="point_radio" value="{$admin.point_radio}" lay-verify="point_radio" autocomplete="off" placeholder="请输入积分比例" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 例：0.5表示50%，即消费200赠送100积分
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">商户号</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="shopNo" value="{$admin.shopNo}" lay-verify="shopNo" autocomplete="off" placeholder="请输入商户号" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 拉卡拉分配的商户号，请勿随意修改，否则支付会出问题。
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">终端号</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="termId" value="{$admin.termId}" lay-verify="termId" autocomplete="off" placeholder="请输入终端号" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 拉卡拉分配的终端号，请勿随意修改，否则支付会出问题。
        </div>
    </div>

    <div class="layui-form-item" style="display: flex;">
        <label class="layui-form-label">缩略图</label>
        <div class="layui-input-inline">
            <div class="fup_files">
                <input type="file" name="logo" onchange="fuploads($(this))">
                <img src="{$admin.logo}" class="img" name="img" width="100%" height="100%"/>
            </div>
        </div>
    </div>

    <div class="layui-form-item" id="x-city">
        <label class="layui-form-label">地区</label>
        <div class="layui-input-inline">
            <select name="province" lay-filter="province">
                <option value="">请选择省</option>
            </select>
        </div>
        <div class="layui-input-inline">
            <select name="city" lay-filter="city">
                <option value="">请选择市</option>
            </select>
        </div>
        <div class="layui-input-inline">
            <select name="area" lay-filter="area">
                <option value="">请选择县/区</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">详细地址</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="address" value="{$admin.address}" lay-verify="address" autocomplete="off" placeholder="请输入详细地址" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
            <a href="javascript:history.back(-1)" class="layui-btn layui-btn-primary">返回</a>
        </div>
    </div>
</form>
          
<script src="/static/temp/layui/layui.js" charset="utf-8"></script>
<script src="/static/temp/js/xcity.js" type="text/javascript"></script>

<script>
    //图片上传
    function fuploads(f){
        var nameAll = f[0].files[0].type;
        var name = nameAll.split("/");
        
        if(name[0] != 'image'){
            layer.msg('上传类型错误', {icon: 2});
        }else{
            var url = window.URL.createObjectURL(f[0].files[0]);  // 得到bolb对象路径，可当成普通的文件路径一样使用
            f.next("img").css('display', 'block');
            f.next("img").attr('src', url);
        }
    }

    layui.use(['form'], function(){
        form = layui.form
        layer = layui.layer

        $('#x-city').xcity(`{$admin.province}`, `{$admin.city}`, `{$admin.area}`);

        // 监听提交
        form.on('submit(submit)', function(data){
            // 加载中样式... 
            let loading = layer.msg('正在修改，请稍后...', {icon: 16, shade: 0.3, time:0});

            let formData = new FormData();
            let img = $("input[name='logo");

            formData.append('id', data.field.id);
            formData.append('nickname', data.field.nickname);
            formData.append('username', data.field.username);
            formData.append('password', data.field.password);
            formData.append('money_gold', data.field.money_gold);
            formData.append('point_radio', data.field.point_radio);
            formData.append('province', data.field.province);
            formData.append('city', data.field.city);
            formData.append('area', data.field.area);
            formData.append('address', data.field.address);
            formData.append('gold_time', data.field.gold_time);
            formData.append('shopNo', data.field.shopNo);
            formData.append('termId', data.field.termId);
            formData.append('logo', img[0].files[0]);

            $.ajax({
                url:'{:url("Shop/shop_edit_data")}',
                data:formData,
                contentType: false,  // 必须false才会自动加上正确的Content-Type
                processData: false,
                type:"post",
                dataType:'json',
                success:function(res){
                    layer.close(loading);
                    if(res.code == 0){
                        if(res.data == 0){
                            window.location.reload()
                        }else{
                            layer.msg(res.msg, {icon: 1,time: 1500}, function(){
                                // window.location.href = "{:url('Shop/shop_list')}";
                                history.back(-1);
                            });
                        }
                    }else{
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error:function(res){
                    layer.msg(res.msg, {icon: 2});
                }
            })

            // $.post('{:url("Shop/shop_edit_data")}', data.field, function(res){
            //     layer.close(loading);
            //     if(res.code == 0){
            //         layer.msg(res.msg, {icon: 1,time: 1500}, function(){
            //             window.location.href = "{:url('Shop/shop_list')}";
            //         });
            //     }else{
            //         layer.msg(res.msg, {icon: 2});
            //     }
            // })

            return false;
        });
    });
</script>
</body>
</html>